/*
 * @Author: zheguo 3188606474@qq.com
 * @Date: 2024-04-19 18:54:36
 * @LastEditors: zheguo 3188606474@qq.com
 * @LastEditTime: 2024-04-24 22:36:32
 * @FilePath: \ProjectAction\projectOne\myexpress\app\controllers\admin\admin.controller.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const db = require("../../models/index")
const Admins = db.admins
// 通过用户名、密码、身份查询一条数据
exports.findone = (req, res) => {
    Admins.findOne({
        raw: true,
        where: {
            username: req.query.username,
            password: req.query.password,
            identify: req.query.identify
        }
    }).then((data) => {
        if (data) {
            res.send({data: data, code: 103, msg: "登录成功"})
            console.log(data)
        } else {
            res.send({data: null, code: 203, msg: "登录失败"})
        }
    }).catch(err => {
        console.log(err.message + "查询数据出错")

    })
}
// 查询所有数据
exports.findAll = (req, res) => {
    Admins.findAll().then((data) => {
        res.send({
            data: data,
            code: 101,
            msg: "查询成功"
        })
    }).catch(err => {
        console.log(err.message + "查询数据出错")

    })
}
// 通过用户名，密码，验证码，备注创建数据
exports.create = (req, res) => {
    const dataTable = {
        username: req.body.adminname,
        password: req.body.password,
        identify: req.body.identify,
        imgPath: req.body.imgPath,
    }

    Admins.create(dataTable).then(data => {
        if (data) {
            res.send({data: data, code: 104, message: "注册成功"})
            console.log(data)
        } else {
            res.send({data: null, code: 204, message: "注册失败"})
        }
    }).catch(err => {
        console.log(err.message + "创建数据出错")
    })
}
// 通过id更新数据
exports.update = (req, res) => {
    Admins.update({
        username: req.body.username,
        password: req.body.password,
        identify: req.body.identify,
        remarks: req.body.remarks
    }, {
        where: {
            id: req.body.id
        }
    }).then(data => {
        if (data[0] !== 0) {
            res.send({ok: 1})
        } else {
            res.send({ok: 0})
        }
    }).catch(err => {
        console.log(err)
    })
}

// 通过id删除数据
exports.delete = (req, res) => {

    Admins.destroy({
        where: {
            id: req.query.id
        }
    }).then(data => {
        res.send({ok: 1})
    }).catch(err => {
        res.send({ok: 0})
    })
}